package user.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.dao.UserDao;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MessageUpdate extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;
	private static String loginName = null;
	private static String password = null;
	private UserDao userDao = new UserDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MessageUpdate frame = new MessageUpdate();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MessageUpdate() {
		setTitle("修改密码");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 329);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel Label = new JLabel("用户名");
		Label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		textField = new JTextField();
		loginName = LoginFrame.userName();
		textField.setText(loginName);
		textField.setEditable(false);
		textField.setColumns(10);
		
		JLabel Label1 = new JLabel("原密码");
		Label1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel Label2 = new JLabel("新密码");
		Label2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		passwordField = new JPasswordField();
		
		passwordField_1 = new JPasswordField();
		
		JLabel Label3 = new JLabel("确认密码");
		Label3.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		passwordField_2 = new JPasswordField();
		
		JButton Button2 = new JButton("取消");
		Button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		Button2.setForeground(SystemColor.textHighlight);
		Button2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton Button1 = new JButton("确认");
		Button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateActionPerformed(e);
			}
		});
		Button1.setForeground(SystemColor.textHighlight);
		Button1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(Button2))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(58)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(Label3)
								.addComponent(Label, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
								.addComponent(Label1)
								.addComponent(Label2))))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addComponent(passwordField)
									.addComponent(textField, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
								.addComponent(passwordField_1, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
								.addComponent(passwordField_2, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
							.addContainerGap(128, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(Button1)
							.addGap(92))))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(37)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label1)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label2)
						.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Label3)
						.addComponent(passwordField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(Button2)
						.addComponent(Button1))
					.addContainerGap())
		);
		this.setLocationRelativeTo(null);
		contentPane.setLayout(gl_contentPane);
	}

	/**
	 * 修改密码事件的处理
	 * @param e
	 */
	protected void UpdateActionPerformed(ActionEvent e) {
		//拿到输入的值
		String pwd =new String(passwordField.getPassword());
		String newpwd1 = new String(passwordField_1.getPassword());
		String newpwd2 = new String(passwordField_2.getPassword());
		//判断原密码是否相同
		password = LoginFrame.password();
		loginName = LoginFrame.userName();
		if(pwd.equals("")) {
			JOptionPane.showMessageDialog(null, "请输入原密码");
		}
		else if(pwd.equals(password)) {//原密码相同
			if(newpwd1.equals(newpwd2)) {
				int i = userDao.updatePwd(newpwd1,loginName);
				if(newpwd1.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入新密码");
				}
				if(newpwd2.equals("")){
					JOptionPane.showMessageDialog(null, "请再次输入新密码");
				}
				else if(i>0) {//修改成功
					JOptionPane.showMessageDialog(null, "修改成功！");
					reset();
					this.dispose();
				}
			}else {
				JOptionPane.showMessageDialog(null, "两次密码不一致");
				reset();
			}
		
		}
		else {//原密码不相同
			JOptionPane.showMessageDialog(null, "输入的密码与原密码不相同");
		}
		
	}
	/**
	 * 重置
	 */
	public void reset() {
		passwordField.setText("");
		passwordField_1.setText("");
		passwordField_2.setText("");
	}
}
